<?php
/**
 * Create by Phpstorm
 * User: Peco
 * Date: 2019/12/5
 * Time: 14:42
 */

namespace App\Repositories;

use App\Http\Controllers\Api\BaseAdminController;
use App\Models\SendVerifyCodeLog;
use App\Models\UserAddress;
use App\Models\UserAuthentication;

/**
 * Class UserInfoRepository
 * @package App\Repositories
 */
class UserInfoRepository extends BaseAdminController
{
    /**
     * @var UserAuthentication
     */
    protected $userAuthentication;

    /**
     * @var UserAddress
     */
    protected $userAddress;

    /**
     * @var SendVerifyCodeLog
     */
    protected $sendVerifyCodeLog;

    /**
     * UserInfoController constructor.
     * @param UserAuthentication $userAuthentication
     * @param UserAddress $userAddress
     */
    public function __construct(UserAuthentication $userAuthentication,
                                UserAddress $userAddress,
                                SendVerifyCodeLog $sendVerifyCodeLog)
    {
        $this->userAuthentication = $userAuthentication;
        $this->sendVerifyCodeLog = $sendVerifyCodeLog;
        $this->userAddress = $userAddress;
    }

    /**
     * @param array $data
     * @param int $limit
     * @return mixed
     */
    public function getUserAuthenticationPageRepository(array $data, $limit = LIMIT_PAGE){
        return $this->userAuthentication
            ->with(['user'])
            ->when(!is_null($data['real_status']), function ($q)use($data){
                $q->where('real_status', $data['real_status']);
            })
            ->when($data['realname'], function ($q)use($data){
                $q->where('realname', 'like', '%'.$data['realname'].'%');
            })
            ->when($data['userId'], function ($q)use($data){
                $q->where('userId', $data['userId']);
            })
            ->orderByDesc('updated_at')
            ->paginate($limit);
    }

    /**
     * 获取用户地址列表
     * @param array $data
     * @param int $limit
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getUserAddressPageRepository(array $data, $limit = LIMIT_PAGE){
        return $this->userAddress
            ->with(['user'])
            ->when($data['mobile'], function ($q)use($data){
                $q->where('mobile', $data['mobile']);
            })
            ->when($data['consignee'], function ($q)use($data){
                $q->where('consignee', 'like', '%'.$data['consignee'].'%');
            })
            ->when($data['userId'], function ($q)use($data){
                $q->where('userId', $data['userId']);
            })
            ->orderByDesc('updateTime')
            ->paginate($limit);
    }


}
